/**
* @description: 852. 山脉数组的峰顶索引
* @author hewei
* @date 2022/9/7 16:21
* @version 1.0
*/

public class PeakIndexInMountainArray {

    public int peakIndexInMountainArray(int[] arr) {
        int l = 0;
        int n = arr.length;
        int r = n - 1;
        while (l <= r) {
            int mid = (r - l) / 2 + l;
            if (mid == 0) {
                l = mid + 1;
            } else if (mid == n - 1) {
                r = mid - 1;
            } else if (arr[mid - 1] < arr[mid] && arr[mid] < arr[mid + 1]) {
                l = mid + 1;
            } else if (arr[mid + 1] < arr[mid] && arr[mid - 1] > arr[mid]) {
                r = mid - 1;
            } else return mid;
        }
        return l;
    }
}
